﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using TMV.Repositories;
using TMV.UnitOfWork;

namespace TMV.Services
{
    public abstract class ApplicationService<T> : IApplicationService<T> where T : class
    {
        IUnitOfWork unitOfWork;
        IApplicationRepository<T> repository;

        public ApplicationService(IUnitOfWork unitOfWork, IApplicationRepository<T> repository)
        {
            this.unitOfWork = unitOfWork;
            this.repository = repository;
        }

        public virtual IEnumerable<T> Get(
            Expression<Func<T, bool>> filter = null,
            Func<IQueryable<T>,
            IOrderedQueryable<T>> orderBy = null,
            string includeProperties = "")
        {
            return repository.Get(filter, orderBy, includeProperties);
        }

        public virtual T GetById(object id)
        {
            return repository.GetById(id);
        }

        public virtual T Insert(T role)
        {
            var entity = repository.Insert(role);
            unitOfWork.Save();
            return entity;
        }

        public virtual IEnumerable<T> Insert(IEnumerable<T> roles)
        {
            var entities = repository.Insert(roles);
            unitOfWork.Save();
            return entities;
        }

        public virtual void Update(T role)
        {
            repository.Update(role);
            unitOfWork.Save();
        }

        public virtual void Delete(T role)
        {
            repository.Delete(role);
            unitOfWork.Save();
        }

        public virtual void Delete(object id)
        {
            repository.Delete(id);
            unitOfWork.Save();
        }

        public void Dispose()
        {
            this.unitOfWork.Dispose();
        }
    }
}